﻿using Ewell.BizComponent.UI.SearchPersonCom.ViewModel;
using Ewell.Interface.Common.Model.Common;
using Ewell.Interface.Common.Model.GateWay.Dto.Dict;
using Ewell.Interface.Common.Model.GateWay.GateWayUriInfo;
using Ewell.Interface.Common.Utils;
using MahApps.Metro.Controls;
using Newtonsoft.Json.Linq;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Input;

namespace Ewell.BizComponent.UI.SearchPersonCom.Views
{
    /// <summary>
    /// BizSearchView.xaml 的交互逻辑
    /// </summary>
    public partial class SearchPersonComView : UserControl
    {
        public static readonly DependencyProperty SearchPersonComVmProperty = DependencyProperty
            .Register("searchPersonComVm", typeof(SearchPersonComVm), typeof(SearchPersonComView),
            new FrameworkPropertyMetadata(new SearchPersonComVm()));
        public SearchPersonComVm searchPersonComVm
        {
            get
            {
                if (this.DataContext?.GetType() != typeof(SearchPersonComVm))
                    return (SearchPersonComVm)GetValue(SearchPersonComVmProperty);
                else
                    return this.DataContext as SearchPersonComVm;
            }
            set { SetValue(SearchPersonComVmProperty, value); }
        }
        /// <summary>
        /// 组件代码(读卡组件6)
        /// </summary>
        public string componentCoding { get; set; }
        /// <summary>
        /// 医保读卡展示
        /// </summary>
        public bool useInsurContent { get; set; }
        public SearchPersonComView()
        {
            InitializeComponent();
            this.DataContext = this.searchPersonComVm;
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            if (useInsurContent)
                insurContent.Visibility = Visibility.Visible;
            else
                insurContent.Visibility = Visibility.Collapsed;
            //查询检索组件
            QueryBizSearchComponentListReq req = new QueryBizSearchComponentListReq()
            {
                componentCoding = componentCoding,
                enabledFlag = "1",
            };
            BizSearchComponentDubboService bizSearchComponentDubboService = new BizSearchComponentDubboService();
            var gateWayResult = bizSearchComponentDubboService.queryBizSearchComponentListByCoding.Post<QueryBizSearchComponentListResp>(req).CheckState();
            if (gateWayResult.@object?.bizSearchTypeList == null || gateWayResult.@object?.bizSearchTypeList?.Count == 0)
                throw new SpeedException("读卡组件设置错误!");
            List<SearchPersonComBtnVm> buttonComponents = new List<SearchPersonComBtnVm>();
            foreach (var item in gateWayResult.@object.bizSearchTypeList)
            {
                buttonComponents.Add(item.CopyProperties<SearchPersonComBtnVm>());
            }
            this.searchPersonComVm.searchPersonComBtnList = buttonComponents;
            this.searchPersonComVm.searchPersonComBtnList.FirstOrDefault().isChecked = true;
        }

        private void SearchTextKeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                searchPersonComVm.searchText = (sender as TextBox).Text;
                var buttonComponent = this.searchPersonComVm.searchPersonComBtnList.FirstOrDefault(f => f.isChecked && f.triggerTypeCoding == "3");
                if (buttonComponent != null)
                {
                    searchPersonComVm.readCardCmd.Execute(((sender as TextBox).DataContext as SearchPersonComVm).searchPersonComBtnList.FirstOrDefault(f => f.isChecked));
                }
            }
        }

        private void SearchButton_Click(object sender, RoutedEventArgs e)
        {
            this.searchPersonComVm.searchPersonComBtnList.ForEach(e => e.isChecked = false);
            ((sender as ToggleButton).DataContext as SearchPersonComBtnVm).isChecked = true;
            this.searchPersonComVm.searchTextMark = ((sender as ToggleButton).DataContext as SearchPersonComBtnVm).inputHintContent;
            searchTextBox.Focus();
            if (((sender as ToggleButton).DataContext as SearchPersonComBtnVm).triggerTypeCoding == "2")
                searchPersonComVm.readCardCmd.Execute(((sender as ToggleButton).DataContext as SearchPersonComBtnVm));
        }

        private void DataGrid_LoadingRow(object sender, DataGridRowEventArgs e)
        {
            List<object> list = (sender as DataGrid).ItemsSource as List<object>;
            object obj = list.FirstOrDefault();
            if (obj != null)
            {
                var keyValuePairs = obj as JObject;
                if (keyValuePairs != null)
                {
                    foreach (var keyValuePair in keyValuePairs)
                    {
                        foreach (var column in (sender as DataGrid).Columns)
                        {
                            if (column.Header.Equals(keyValuePair.Key))
                                column.Header = (DataContext as SearchPersonComVm).medPatientExt.titleAndAttribute.FirstOrDefault(f => f.dataIndex == keyValuePair.Key).title;
                        }
                    }
                }
            }

        }

        private void MetroAnimatedTabControl_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if ((sender as MetroAnimatedTabControl).SelectedIndex == -1)
                (sender as MetroAnimatedTabControl).SelectedIndex = 0;
        }
    }
}
